package com.dormitory.management.system.utils;

import org.apache.commons.io.FileUtils;
import org.springframework.web.multipart.MultipartFile;
import ws.schild.jave.EncoderException;
import ws.schild.jave.MultimediaInfo;
import ws.schild.jave.MultimediaObject;

import java.io.File;
import java.io.IOException;

public class  VideoUtil {
    /**
     * 获取视频时间长度
     * @param multipartFile 视频文件
     * @return 视频时间长度 （maximum (104857600)）
     */
    public static long getVideoTime(MultipartFile multipartFile) throws IOException {
        File file = new File(multipartFile.getOriginalFilename());
        FileUtils.copyInputStreamToFile(multipartFile.getInputStream(),file);
        MultimediaObject instance=new MultimediaObject(file);
        MultimediaInfo info = null;
        try {
            info = instance.getInfo();
            long videoTime=info.getDuration()/1000;
            file.delete();
            return videoTime;
        } catch (EncoderException e) {
            e.printStackTrace();
        }
        return 0L;
    }
}
